package voja.android.log;

import java.util.Date;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.provider.CallLog.Calls;
import android.text.format.DateFormat;
import android.view.View;

public class Util {
	public static String getType(int typeId){
		String type="";
		switch(typeId){
			case Calls.INCOMING_TYPE:
				type = "RECEIVED";
				break;
			case Calls.OUTGOING_TYPE:
				type = "PLACED";
				break;
			case Calls.MISSED_TYPE:
				type = "MISSED";
				break;
		}
		return type;
	}
	
	public static String plural(int count, String sing, String plur){
		String decimal = ("" + (Double.parseDouble(""+count)/10));
		int dec = Integer.parseInt(decimal.substring(decimal.lastIndexOf(".")+1));
		switch(dec){
			default:
				return plur;
			case 1:
				return sing;
		}
	}
    
    public static String timeFormat(long sec){
		double tmp=0;
		int h = (int)Math.floor(sec/3600);
		tmp = sec % 3600;
		
		int m = (int)Math.floor(tmp/60);
		tmp = tmp % 60;
		
		return (h>9 ? h : "0"+h) + ":" + (m>9 ? m : "0"+m) + ":" + (tmp>9 ? (int)tmp : "0"+(int)tmp);
	}
    
    public static String dateFromTimestamp(long timestamp, String format){
		Date date = new Date();
		date.setTime(timestamp);
		return (String)DateFormat.format(format, date);
	}
    
    public static String join(String[] arr, String glue){
		String ret = "";
		for(int i=0; i<arr.length; i++){
			if(i>0) ret += glue;
			ret += arr[i];
		}
		return ret;
	}
    
    public static void alert(Context c, String message){
    	Util.alert(c, message, "Message");
    }
    public static void alert(Context c, String message, String title){
    	Builder a = new AlertDialog.Builder(c);
    	a.setTitle(title);
    	a.setMessage(message);
    	a.setPositiveButton("Close", null);
    	a.show();
    }
    
    public static void error(MainActivity a, String message){
    	//a.er.setText(message);
		//a.er.setVisibility(View.VISIBLE);
    	Util.alert(a.context, message, "Error!");
    }
}
